home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / fontutil.6 / fontutil / fontutils-0.6 / charspace / output.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-14  |  10.6 KB  |  363 lines

  1. /* output.c: use the `char_type's' information to output a new font.
  2.  
  3. Copyright (C) 1992 Free Software Foundation, Inc.
  4.  
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "config.h"
  20.  
  21. #include "encoding.h"               
  22. #include "filename.h"
  23. #include "gf.h"
  24. #include "libfile.h"
  25. #include "report.h"
  26. #include "tfm.h"
  27.  
  28. #include "char.h"
  29. #include "main.h"
  30. #include "output.h"
  31. #include "symtab.h"
  32.  
  33.  
  34. /* The assignments to the TFM fontdimen values.  (-fontdimens)  */
  35. string fontdimens = NULL;
  36.  
  37. /* The name of the output file specified by the user.  (-output-file)  */
  38. string output_name = NULL;
  39.  
  40. /* The character code to use for the x-height.  (-xheight-char)  */
  41. charcode_type xheight_char = 120; /* `x' in ASCII.  */
  42.  
  43.  
  44. static void finalize_kern_list (tfm_char_type *, char_type *[]);
  45. static void finalize_lig_list (tfm_char_type *, char_type *[]);
  46. static tfm_global_info_type find_tfm_info (char_type *[]);
  47. static gf_char_type make_gf_char (char_info_type);
  48. static void update_tfm_info_from_symtab (tfm_global_info_type *);
  49. static void write_gf (bitmap_font_type, char_type *[]);
  50. static void write_tfm (char_type *[]);
  51. static boolean write_tfm_char (charcode_type, char_type *[]);
  52.  
  53. /* Output a TFM and (optionally) GF file with the characters in CHARS. 
  54.    Use the characters from BITMAP_FONT for the shapes.  We use an
  55.    existing TFM file, the encoding file, and command line arguments to
  56.    determine the metric information.  */
  57.  
  58. void
  59. output_font (bitmap_font_type bitmap_font, char_type *chars[])
  60. {
  61.   write_tfm (chars);
  62.   
  63.   if (!no_gf)
  64.     write_gf (bitmap_font, chars);
  65. }
  66.  
  67. /* Write a TFM file with CHARS.  */
  68.  
  69. static void
  70. write_tfm (char_type *chars[])
  71. {
  72.   unsigned this_char;
  73.   unsigned char_count = 0;
  74.   string pl_output_name = extend_filename (output_name, "pl");
  75.   tfm_global_info_type tfm_info = find_tfm_info (chars);
  76.  
  77.   /* If `have_tfm', we've already opened the input file.  */
  78.   string input_tfm_name = have_tfm ? tfm_input_filename () : NULL;
  79.   
  80.   /* We remove the suffix before making the PL name because if the user
  81.      said `-output-file=foo.bar', it's the TFM file which should be
  82.      named `foo.bar'; the PL file is still named `foo.pl'.  */
  83.   string output_root = remove_suffix (output_name);
  84.   string pl_name = extend_filename (output_root, "pl");
  85.   string output_tfm_name = extend_filename (output_name, "tfm");
  86.  
  87.   /* If we'd overwrite the input, change the name.  */
  88.   if (input_tfm_name != NULL && same_file_p (input_tfm_name, output_tfm_name))
  89.     {
  90.       pl_name = make_prefix ("x", pl_name);
  91.       output_tfm_name = make_prefix ("x", output_tfm_name);
  92.     }
  93.     
  94.   /* Let's write the TFM file first, but to a PL file, which is easier.  */
  95.   if (!tfm_open_pl_output_file (pl_output_name))
  96.     FATAL_PERROR (pl_output_name);
  97.  
  98.   REPORT1 ("\nWriting PL file `%s':\n", pl_output_name);
  99.  
  100.   /* Write the global information we just figured out.  */
  101.   tfm_put_global_info (tfm_info);
  102.  
  103.   /* Only look between `starting_char' and `ending_char', i.e., the
  104.      user's -range.  */
  105.   for (this_char = starting_char; this_char <= ending_char; this_char++)
  106.     {
  107.       if (write_tfm_char (this_char, chars))
  108.         REPORT2 ("[%d]%c", this_char, ++char_count % 13 == 0 ? '\n' : ' ');
  109.     }
  110.  
  111.   if (char_count % 13 != 0)
  112.     REPORT ("\n");
  113.  
  114.   tfm_convert_pl (output_tfm_name, verbose);
  115.   tfm_close_pl_output_file ();
  116. }
  117.  
  118.  
  119. /* Return the global TFM information structure: we read from an existing
  120.    TFM file, if the global `have_tfm' is true, then from the encoding
  121.    information in the global `encoding_info', then from the symbol
  122.    table, and finally from the command line (i.e., the global
  123.    `fontdimens).  */
  124.  
  125. static tfm_global_info_type
  126. find_tfm_info (char_type *chars[])
  127. {
  128.   tfm_global_info_type tfm_info;
  129.   
  130.   if (have_tfm)
  131.     {
  132.       tfm_info = tfm_get_global_info ();
  133.       TFM_CHECKSUM (tfm_info) = 0; /* Can't use the old checksum.  */
  134.     }
  135.   else
  136.     { /* No TFM file to be found, so initialize the structure
  137.          with the defaults.  */
  138.       tfm_info = tfm_init_global_info ();
  139.       TFM_DESIGN_SIZE (tfm_info) = get_designsize_in_points ();
  140.     }
  141.  
  142.   /* `encoding_info' has been set at this point, one way or another; use
  143.      whatever codingscheme it says.  */
  144.   TFM_CODING_SCHEME (tfm_info) = ENCODING_SCHEME_NAME (encoding_info);
  145.  
  146.   /* The `fontsize' fontdimen is typically the same as the designsize.
  147.      (I can't imagine when it would be anything else, but it seems
  148.      prudent to let the user-specified values in the symbol table or
  149.      commandline override this.)  */
  150.   tfm_set_fontsize (&tfm_info);
  151.   
  152.   /* Use the bitmap height of `xheight_char', if it exists, for the
  153.      default x-height.  If not, guess .5 of the designsize. */
  154.   TFM_FONTDIMEN (tfm_info, TFM_XHEIGHT_PARAMETER)
  155.     = chars[xheight_char] != NULL
  156.        ? PIXELS_TO_POINTS (
  157.               CHAR_HEIGHT (*CHAR_BITMAP_INFO (*chars[xheight_char])), dpi_real)
  158.        : .5 * TFM_DESIGN_SIZE (tfm_info);
  159.   
  160.   /* If any fontdimens were set in the CMI files, use them.  */
  161.   update_tfm_info_from_symtab (&tfm_info);
  162.   
  163.   /* Finally, update the fontdimens from the command line.  */
  164.   tfm_set_fontdimens (fontdimens, &tfm_info);
  165.  
  166.   return tfm_info;
  167. }
  168.  
  169.  
  170. /* It is not an error for the user to use a fontdimen name for something
  171.    else, so we do not complain if we cannot resolve such a name.  */
  172.  
  173. static void
  174. update_tfm_info_from_symtab (tfm_global_info_type *tfm_info)
  175. {
  176.   unsigned p;
  177.   
  178.   for (p = 1; p <= TFM_MAX_FONTDIMENS; p++)
  179.     {
  180.       string name = tfm_fontdimen_name (p);
  181.       symval_type *sv = name ? symtab_lookup (name) : NULL;
  182.       if (sv != NULL)
  183.         {
  184.           if (symval_resolve (sv))
  185.             tfm_set_fontdimen (tfm_info, p,
  186.                            PIXELS_TO_POINTS (SYMVAL_REAL (*sv), dpi_real));
  187.     }
  188.     }
  189. }
  190.  
  191.  
  192. /* Return true if we have TFM info for CODE in CHARS.  */
  193.  
  194. static boolean
  195. have_tfm_char (charcode_type code, char_type *chars[])
  196. {
  197.   boolean ok;
  198.   
  199.   /* If we have no `char_type' for CODE, have to give up.  */
  200.   if (chars[code] == NULL)
  201.     ok = false;
  202.   
  203.   /* If we have no TFM info in the `char_type', have to give up.  */
  204.   else if (CHAR_TFM_INFO (*chars[code]) == NULL)
  205.     ok = false;
  206.   
  207. /* xx what about _EXISTS? */
  208.   
  209.   else
  210.     ok = true;
  211.   
  212.   return ok;
  213. }
  214.  
  215.  
  216. /* Write the single character CODE from CHARS to the TFM file (assume
  217.    the latter is open), if we have all the information.  Return true if
  218.    we wrote it, else false.  */
  219.  
  220. static boolean
  221. write_tfm_char (charcode_type code, char_type *chars[])
  222. {
  223.   boolean ok;
  224.   
  225.   if (have_tfm_char (code, chars))
  226.     {
  227.       tfm_char_type c = *CHAR_TFM_INFO (*chars[code]);
  228.  
  229.       /* The remaining thing to check is that the kerns and ligatures
  230.          refer only to (other) characters which will be output.  */
  231.       finalize_kern_list (&c, chars);
  232.       finalize_lig_list (&c, chars);
  233.  
  234.       /* Write the character.  */
  235.       tfm_put_char (c);
  236.       
  237.       ok = true;
  238.     }
  239.   else
  240.     ok = false;
  241.  
  242.   return ok;
  243. }
  244.  
  245.  
  246. /* Look through the kern list in C for kerns which refer to characters
  247.    in CHARS we won't be outputting.  Remove such.  */
  248.  
  249. static void
  250. finalize_kern_list (tfm_char_type *c, char_type *chars[])
  251. {
  252.   unsigned e;
  253.   list_type good_list = list_init ();
  254.  
  255.   for (e = 0; e < LIST_SIZE (TFM_KERN (*c)); e++)
  256.     {
  257.       tfm_kern_type *k = LIST_ELT (TFM_KERN (*c), e);
  258.  
  259.       if (have_tfm_char (k->character, chars))
  260.         {
  261.           tfm_kern_type *new_kern = LIST_TAPPEND (&good_list, tfm_kern_type);
  262.           *new_kern = *k;
  263.     }
  264.     }
  265.   
  266.   /* Throw away the old list.  */
  267.   list_free (&TFM_KERN (*c));
  268.   
  269.   /* Insert the good list.  */
  270.   TFM_KERN (*c) = good_list;
  271. }
  272.  
  273.  
  274. /* Routine for ligatures analogous to `finalize_kern_list'.  */
  275.  
  276. static void
  277. finalize_lig_list (tfm_char_type *c, char_type *chars[])
  278. {
  279.   unsigned e;
  280.   list_type good_list = list_init ();
  281.  
  282.   for (e = 0; e < LIST_SIZE (TFM_LIGATURE (*c)); e++)
  283.     {
  284.       tfm_ligature_type *lig = LIST_ELT (TFM_LIGATURE (*c), e);
  285.  
  286.       if (have_tfm_char (lig->character, chars)
  287.           && have_tfm_char (lig->ligature, chars))
  288.         {
  289.           tfm_ligature_type *new_lig
  290.           = LIST_TAPPEND (&good_list, tfm_ligature_type);
  291.           *new_lig = *lig;
  292.     }
  293.     }
  294.   
  295.   /* Throw away the old list.  */
  296.   list_free (&TFM_LIGATURE (*c));
  297.   
  298.   /* Insert the good list.  */
  299.   TFM_LIGATURE (*c) = good_list;
  300. }
  301.  
  302. /* Write a GF file using BITMAP_FONT and CHARS.  Don't overwrite an
  303.    existing file named `BITMAP_FONT_FILENAME (FONT)'.  */
  304.  
  305. static void
  306. write_gf (bitmap_font_type font, char_type *chars[])
  307. {
  308.   unsigned this_char;
  309.   unsigned char_count = 0;
  310.   /* We depend upon `output_name' being set when we are called.  */
  311.   string gf_output_name = extend_filename (output_name, concat (dpi, "gf"));
  312.  
  313.   if (same_file_p (BITMAP_FONT_FILENAME (font), gf_output_name))
  314.     gf_output_name = make_prefix ("x", gf_output_name);
  315.  
  316.   if (!gf_open_output_file (gf_output_name))
  317.     FATAL_PERROR (gf_output_name);
  318.  
  319.   REPORT1 ("\nWriting GF file `%s':\n", gf_output_name);
  320.  
  321.   /* Write the first part of the file.  */
  322.   gf_put_preamble (concat4 ("charspace output ", now () + 4, " from ",
  323.                             BITMAP_FONT_COMMENT (font)));
  324.  
  325.   for (this_char = starting_char; this_char <= ending_char; this_char++)
  326.     {
  327.       if (chars[this_char] != NULL
  328.           && CHAR_BITMAP_INFO (*chars[this_char]) != NULL)
  329.         {
  330.           gf_put_char (make_gf_char (*CHAR_BITMAP_INFO (*chars[this_char])));
  331.           REPORT2 ("[%d]%c", this_char, ++char_count % 13 == 0 ? '\n' : ' ');
  332.         }
  333.     }
  334.  
  335.   if (char_count % 13 != 0)
  336.     REPORT ("\n");
  337.  
  338.   /* Write the last part of the file.  */
  339.   gf_put_postamble (real_to_fix (get_designsize_in_points ()),
  340.                     dpi_real, dpi_real);
  341.  
  342.   gf_close_output_file ();
  343. }
  344.  
  345.  
  346. /* Turn BITMAP_CHAR (the generic structure) into a `gf_char_type'.  We
  347.    assume there are no real changes to be made in BITMAP_CHAR, i.e.,
  348.    that we need only copy the relevant fields.  */
  349.  
  350. static gf_char_type
  351. make_gf_char (char_info_type bitmap_char)
  352. {
  353.   gf_char_type gf_char;
  354.   
  355.   GF_CHARCODE (gf_char) = CHARCODE (bitmap_char);
  356.   GF_BITMAP (gf_char) = CHAR_BITMAP (bitmap_char);
  357.   GF_CHAR_BB (gf_char) = CHAR_BB (bitmap_char);
  358.   GF_H_ESCAPEMENT (gf_char) = CHAR_SET_WIDTH (bitmap_char);
  359.   GF_TFM_WIDTH (gf_char) = CHAR_TFM_WIDTH (bitmap_char);
  360.   
  361.   return gf_char;
  362. }
  363.